<?php


class WacFinalClothQcTestFormItemTable extends WacCommonTable
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('WacFinalClothQcTestFormItem');
    }

    /*
     * can $objEditItem as edit object
     */
    public function createFromDispatchOrderItem($parentId, $dispatchOrderItemId, $params=array(),$objEditItem=null)
    {
        $objDispatchOrderItem = Doctrine::getTable(WacTable::$dispatchOrderItem)->findOneBy("id", $dispatchOrderItemId);
        if($objDispatchOrderItem){
            if(!is_null($objEditItem)){
                $objSubItem = $objEditItem;
            }
            else{
                $objSubItem = Doctrine::getTable(WacTable::$finalClothQcTestFormItem)->create();
            }
            $objSubItem->setParentId($parentId);
            $objSubItem->setDispatchOrderItemId($dispatchOrderItemId);
            $objSubItem->setGoodsId($objDispatchOrderItem->getGoodsId());
            $objSubItem->setGoodsCategoryId($objDispatchOrderItem->getGoodsCategoryId());
            $objSubItem->setQcGoodsNumber($params["qc_goods_number"]);
            $objSubItem->setDqGoodsNumber($params["dq_goods_number"]);
            $objSubItem->setGoodsNumberUnitCode($objDispatchOrderItem->getGoodsNumberUnitCode());
            $objSubItem->setRoll($params["roll"]);
            $objSubItem->setWeight($params["weight"]);
            $objSubItem->setVerticalShrink($params["vertical_shrink"]);
            $objSubItem->setHorizontalShrink($params["horizontal_shrink"]);
            $objSubItem->setDensity($params["density"]);
//            $objSubItem->setQualityCode($params["quality_code"]);
            $objSubItem->setBeveled($params["beveled"]);
            
            $objSubItem->save();
            return $objSubItem;
        }
        else{
            return false;
        }
    }

    /*
     * return a custom list
     */
    public function getCustomList($arrParams, $page=1, $maxPerPage=20, $isArr=true)
    {
       $objQuery = $this->createQuery('t1')
                ->select('t1.*')
                ->from("WacFinalClothQcTestFormItem t1")
               ->leftJoin("t1.WacParent wp")
                ->leftJoin("t1.WacGoods wg");

        if(is_array($arrParams) && count($arrParams)>0)
        {
            QueryHelper::processOption($objQuery, $arrParams);  // dont set "offset" and "limit" option in it
        }

        $this->_pager = new Doctrine_Pager($objQuery, $page, $maxPerPage);
        if($isArr)
        {
            $items = $this->_pager->execute(array(), Doctrine::HYDRATE_ARRAY);
        }
        else
        {
            $items = $this->_pager->execute();
        }
        $objQuery->free();
        return $items;
    }

    public function findOneByFormAndGoods($form, $goods, $isArr=false)
    {
        $formId = is_object($form) ? $form->getId() : $form;
        $goodsId = is_object($goods) ? $goods->getId() : $goods;

        $objQuery = $this->createQuery('t1')
                ->select('t1.*')
                ->from("WacFinalClothQcTestFormItem t1")
                ->where("t1.parent_id=? and t1.goods_id=?", array($formId, $goodsId));

        if($isArr)
        {
            $dataResult = $objQuery->fetchOne(array(), Doctrine::HYDRATE_ARRAY);
        }
        else{
            $dataResult = $objQuery->fetchOne();
        }

        $objQuery->free();
        return $dataResult;
    }
}